###################################
# Title: The ‘Tough-on-Crime’ Left: Race, Gender, and
######## Elections of Law-and-Order Democrats
# Authors: Isabel Laterzo-Tingley & Leah Christiani
# Date: May 2024

# File: Main Text Results
#####################################

#clear global environ
rm(list=ls())

#packages
library(tidyverse)
library(naniar)
library(cregg)
library(ggplot2)
#library(scales)

#working rd
setwd("/Users/IsabelLaterzo/Dropbox/Research Projects/Tough-on-Crime, Race, Gender Paper - L. Christiani and I. Laterzo/Data/Replication Archive")

#read in data
data <- readRDS("survey1_data.rds")


#############################################################################
############################### DATA SET UP ################################
#############################################################################

# total respondents
total_n <- data %>% 
  select(id) %>%
  unique()

# Isolate Democrats/Democrat Leaners
dem_data <- data %>% filter(dems == 1)

#respondents?
dem_total <- dem_data %>%
  select(id) %>%
  unique()

# Isolate Republicans/Republican Leaners
rep_data <- data %>% filter(reps == 1)

#respondents?
rep_total <- rep_data %>%
  select(id) %>%
  unique()

#create subset of data for white respondents
whiteresp_data <- data %>% filter(race_white == 1)

#create subset of data for Black respondents
blackresp_data <- data %>% filter(race_black == 1)

# create subset of data for female-identifying respondents
female_data <- data %>% filter(female == 1)

# create subset of data for non-female-identifying respondents
male_data <- data %>% filter(female == 0)



#############################################################################
################################ MODELS #####################################
#############################################################################

#### Figure 1: Prefences for Crime Platforms by Partisanship

base_model <- cj(data,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "mm",
                 alpha = .1)

dem_model <- cj(dem_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "mm",
                alpha = .1)

rep_model <- cj(rep_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "mm",
                alpha = .1)


### combining
base_model$sample <- "Pooled"
dem_model$sample <- "Democrats"
rep_model$sample <- "Republicans"
base_pooled <- rbind(base_model, dem_model, rep_model)
base_pooled$sample <- factor(base_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)



### plot
fig1 <- ggplot(base_pooled, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig1

#ggsave("fig1_plot.png", fig1, width=8, height=3)





#### Figure 2: Prefences for Crime Platforms by Respondent Partisanship and Candidate Race


#marginal means
base_interact_race <- cj(data,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "mm",
                         by = ~ cand_race,
                         alpha = .1)

dem_interact_race <- cj(dem_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_race,
                        alpha = .1)

rep_interact_race <- cj(rep_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_race,
                        alpha = .1)


### combining
base_interact_race$sample <- "Pooled"
dem_interact_race$sample <- "Democrats"
rep_interact_race$sample <- "Republicans"
race_pooled <- rbind(base_interact_race, dem_interact_race,
                     rep_interact_race)
race_pooled$sample <- factor(race_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)



# Figure 2 A - MMs for preferences based on candidate race
fig2a <- ggplot(race_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig2a

#ggsave("fig2a_plot.png", fig2a, width=8, height=3)



# differences in MMs
diff_base_interact_race <- cj(data,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm_diff",
                              by = ~ cand_race,
                              alpha = .1)


diff_dem_interact_race <- cj(dem_data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_race,
                             alpha = .1)

diff_rep_interact_race <- cj(rep_data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_race,
                             alpha = .1)

### combining diff
diff_base_interact_race$sample <- "Pooled"
diff_dem_interact_race$sample <- "Democrats"
diff_rep_interact_race$sample <- "Republicans"
race_diff_total <- rbind(diff_base_interact_race, diff_dem_interact_race,
                         diff_rep_interact_race)
race_diff_total$sample <- factor(race_diff_total$sample, 
                                 levels = c("Pooled", "Democrats",
                                            "Republicans"),
                                 ordered = T)


# plot Figure 2b - the differences in marginal means

fig2b <- ggplot(race_diff_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig2b

#ggsave("fig2b_plot.png", fig2b, width=8, height=2)






#### Figure 3: Prefences for Crime Platforms by Respondent Partisanship and Candidate Gender


#marginal means
base_interact_gen <- cj(data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_gender,
                        alpha = .1)

dem_interact_gen <- cj(dem_data,
                       chosen ~ cand_pubsec,
                       id = ~id,
                       estimate = "mm",
                       by = ~ cand_gender,
                       alpha = .1)

rep_interact_gen <- cj(rep_data,
                       chosen ~ cand_pubsec,
                       id = ~id,
                       estimate = "mm",
                       by = ~ cand_gender,
                       alpha = .1)


### combining
base_interact_gen$sample <- "Pooled"
dem_interact_gen$sample <- "Democrats"
rep_interact_gen$sample <- "Republicans"
gender_pooled <- rbind(base_interact_gen, dem_interact_gen,
                       rep_interact_gen)
gender_pooled$sample <- factor(gender_pooled$sample, 
                               levels = c("Pooled", "Democrats",
                                          "Republicans"),
                               ordered = T)



# Figure 3 A - MMs for preferences based on candidate gender

fig3a <- ggplot(gender_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) + 
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig3a


#ggsave("fig3a_plot.png", fig3a, width=8, height=3)




# differences in MMs
diff_base_interact_gen <- cj(data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_gender,
                             alpha = .1)


diff_dem_interact_gen <- cj(dem_data,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm_diff",
                            by = ~ cand_gender,
                            alpha = .1)

diff_rep_interact_gen <- cj(rep_data,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm_diff",
                            by = ~ cand_gender,
                            alpha = .1)

### combining diff
diff_base_interact_gen$sample <- "Pooled"
diff_dem_interact_gen$sample <- "Democrats"
diff_rep_interact_gen$sample <- "Republicans"
gen_diff_total <- rbind(diff_base_interact_gen, diff_dem_interact_gen,
                        diff_rep_interact_gen)
gen_diff_total$sample <- factor(gen_diff_total$sample, 
                                levels = c("Pooled", "Democrats",
                                           "Republicans"),
                                ordered = T)


# plot Figure 3b - the differences in marginal means

fig3b <- ggplot(gen_diff_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig3b

#ggsave("fig3b_plot.png", fig3b, width=8, height=2)




#### Figure 4: Democrats' Preferences for Crime Platforms by Respondent Partisanship
#### and Candidate Gender


## subset by candidate Race, then examine gender

black_dem_cands <- dem_data %>% filter(cand_race == "Black")
white_dem_cands <- dem_data %>% filter(cand_race == "White")

dem_interact_genblack <- cj(black_dem_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

dem_interact_genwhite <- cj(white_dem_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

### combining
dem_interact_genblack$sample <- "Candidate: Black"
dem_interact_genwhite$sample <- "Candidate: White"
dem_interact_pooled <- rbind(dem_interact_genblack, dem_interact_genwhite)
dem_interact_pooled$sample <- factor(dem_interact_pooled$sample)



# Figure 4 a - second option

fig4a2 <- ggplot(dem_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))


fig4a2

#ggsave("fig4a2_plot.png", fig4a2, width=8, height=3)



# difference in marginal means

diff_dem_interact_genblack <- cj(black_dem_cands,
                                 chosen ~ cand_pubsec,
                                 id = ~id,
                                 estimate = "mm_diff",
                                 by = ~ cand_gender,
                                 alpha = .1)

diff_dem_interact_genwhite <- cj(white_dem_cands,
                                 chosen ~ cand_pubsec,
                                 id = ~id,
                                 estimate = "mm_diff",
                                 by = ~ cand_gender,
                                 alpha = .1)

### combining
diff_dem_interact_genblack$sample <- "Candidate: Black"
diff_dem_interact_genwhite$sample <- "Candidate: White"
diff_dem_interact_total <- rbind(diff_dem_interact_genblack,
                                 diff_dem_interact_genwhite)
diff_dem_interact_total$sample <- factor(diff_dem_interact_total$sample)



fig4b2 <- ggplot(diff_dem_interact_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig4b2

#ggsave("fig4b2_plot.png", fig4b2, width=8, height=2)


